package faw.car.ccenter;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteCallbackList;

import androidx.core.app.NotificationCompat;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import faw.car.ccenter.utils.FLog;
import faw.car.ccenter.utils.ThreadPoolUtil;

public class CCenterService extends Service {
    private static final String TAG = "CCenterService";
    private final ThreadPoolUtil mThreadPool = ThreadPoolUtil.getInstance();
    private final Lock mServiceLock = new ReentrantLock(); //lock for mCarServiceConnectListenerList
    private final RemoteCallbackList<IVehicleCarServiceConnectListener> mCarServiceConnectListenerList =
            new RemoteCallbackList<IVehicleCarServiceConnectListener>();

    private SceneModeManager mSceneModeManager;


    public CCenterService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        FLog.i(TAG, "CCenterService onCreate------>Begin.");

        //初始化线程池
        mThreadPool.init();

        init();

        //writeSystemProperty();
        FLog.i(TAG, "AdapterService onCreate------>End.");
    }

    @Override
    public IBinder onBind(Intent intent) {
        FLog.i(TAG, "AdapterService onBind -- Package: " + intent.getPackage());
        //return mIVehicleRemoteClient;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        FLog.i(TAG, "AdapterService onStartCommand， flags:" + flags + ", startId:" + startId);
        setForeground();
        return START_REDELIVER_INTENT;
    }

    @Override
    public void onDestroy() {
        FLog.i(TAG, "**** AdapterService onDestroy!! ****");
        super.onDestroy();

        //kill mCarServiceConnectListenerList, must unRegisterCarService first!
        mCarServiceConnectListenerList.kill();
        if (mThreadPool != null) {
            mThreadPool.destroy();
        }
        stopForeground(true);
        if (mSceneModeManager != null) {
            mSceneModeManager.destroy();
        }
    }

    private void setForeground() {
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.createNotificationChannel(new NotificationChannel(getPackageName(), getPackageName(),
                NotificationManager.IMPORTANCE_DEFAULT));
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getPackageName());
        startForeground(1, builder.build());
    }

    private void init() {
        FLog.i(TAG, " bindCarService Begin...");
        FLog.i(TAG, " bindCarService End.");
    }

    private void initManagers() {
        FLog.i(TAG, "initManagers() ===================== begin");
        // mCarEOLManager = new CarEOLManager();
//        mThreadPool.execute(() -> {
//            mCarACUManager = new CarACUManager();
//        });
        FLog.i(TAG, "initManagers() ===================== middle CAN finished");

        mSceneModeManager = new SceneModeManager();
        // CarSystemControlManager提前new了，但是其中一些注册方法还是要依赖于carService连接成功，
        // 所以在这里需要等carService连接之后再向carservice注册回调。
        FLog.i(TAG, "initManagers() ===================== end");
    }

//    private final IVehicleRemoteClienter.Stub mIVehicleRemoteClient = new IVehicleRemoteClienter.Stub() {
//
//        @Override
//        public IBinder queryBinder(int binderCode) throws RemoteException {
//            IBinder binder = null;
//            switch (binderCode) {
//                case AdapterConst.BINDER_SCENEMODE:
//                    binder = mSceneModeManager;
//                    break;
//                default:
//                    binder = null;
//                    break;
//            }
//            return binder;
//        }
//        @Override
//        public void registerCarServiceConnectListener(IVehicleCarServiceConnectListener listener) throws RemoteException {
//            FLog.i(TAG, "registerCarServiceConnectListener -- " + listener); //输出注册对象哈希码
//        }
//
//        @Override
//        public void unRegisterCarServiceConnectListener(IVehicleCarServiceConnectListener listener) throws RemoteException {
//            FLog.i(TAG, "unRegisterCarServiceConnectListener -- " + listener);
//        }
//    };


}